package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.organization.model.Account;
import com.wgchao.identityrightsmanagement.domain.organization.repository.AccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.AccountConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: AccountDao
 * @author: wgchao
 * @createTime: 2020/6/12 11:03 AM
 * @Description: TODO
 */
@Component
public class AccountDao implements AccountRepository {
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;
    @Autowired
    private AccountConvert accountConvert;

    @Override
    public List<Account> findByAgency(String agencyCode) {
        List<SysBaseAccount> sysBaseAccounts = sysBaseAccountRepository.findAllByAgencyCodeAndDelIsFalse(agencyCode);
        if(sysBaseAccounts == null || sysBaseAccounts.isEmpty()){
            return null;
        }
        List<Account> accounts = sysBaseAccounts.stream().map(sysBaseAccount -> {
            return accountConvert.convert(sysBaseAccount);
        }).collect(Collectors.toList());
        return accounts;
    }

    @Override
    public List<Account> findByDepartment(String departmentCode) {
        List<SysBaseAccount> sysBaseAccounts = sysBaseAccountRepository.findAllByDepartmentCodeAndDelIsFalse(departmentCode);
        if(sysBaseAccounts == null || sysBaseAccounts.isEmpty()){
            return null;
        }
        List<Account> accounts = sysBaseAccounts.stream().map(sysBaseAccount -> {
            return accountConvert.convert(sysBaseAccount);
        }).collect(Collectors.toList());
        return accounts;
    }

    @Override
    public Account findById(String s) {
        SysBaseAccount sysBaseAccount = sysBaseAccountRepository.findFirstByUsernameAndDelFalse(s);
        if(sysBaseAccount == null){
            return null;
        }
        return accountConvert.convert(sysBaseAccount);
    }

    @Override
    public void remove(Account account, String operator) {
        SysBaseAccount sysBaseAccount = sysBaseAccountRepository.findFirstByUsernameAndDelFalse(account.getUsername());
        if(sysBaseAccount == null){
            throw new ObjectNotExistException("账户不存在");
        }
        sysBaseAccount.setDel(true);
        sysBaseAccount.setLastUpdateTime(new Date());
        sysBaseAccount.setLastUpdator(operator);
        sysBaseAccount = sysBaseAccountRepository.save(sysBaseAccount);
    }

    @Override
    public void add(Account account, String operator) {
        if(account.getId().compareTo(0L) < 0){//新增
            SysBaseAccount sysBaseAccount = sysBaseAccountRepository.findFirstByUsername(account.getUsername());
            if(sysBaseAccount != null){
                throw new ObjectExistException(account.getUsername()+"账户名已经被使用");
            }
            sysBaseAccount = accountConvert.convert(account);
            sysBaseAccount.setCreateTime(new Date());
            sysBaseAccount.setCreator(operator);
            sysBaseAccount.setDel(false);
            sysBaseAccount.setId(null);
            sysBaseAccount = sysBaseAccountRepository.save(sysBaseAccount);
        }else{//修改
            SysBaseAccount sysBaseAccount = sysBaseAccountRepository.findFirstByUsernameAndDelFalse(account.getUsername());
            if(sysBaseAccount == null){
                throw new ObjectNotExistException("账户不存在");
            }
            SysBaseAccount sysBaseAccount_new = accountConvert.convert(account);
            sysBaseAccount_new.setDel(sysBaseAccount.getDel());
            sysBaseAccount_new.setCreator(sysBaseAccount.getCreator());
            sysBaseAccount_new.setCreateTime(sysBaseAccount.getCreateTime());
            sysBaseAccount_new.setLastUpdateTime(new Date());
            sysBaseAccount_new.setLastUpdator(operator);
            sysBaseAccount_new = sysBaseAccountRepository.save(sysBaseAccount_new);
        }

    }

    @Override
    public void addAll(List<Account> accounts, String operator) {
        for(Account account : accounts) {
            add(account, operator);
        }
    }

    @Override
    public void removeAll(List<Account> accounts, String operator) {
        for(Account account : accounts) {
            remove(account, operator);
        }
    }

    @Override
    public void resetDepartment(String departmentCode) {
        List<SysBaseAccount> sysBaseAccounts = sysBaseAccountRepository.findAllByDepartmentCodeAndDelIsFalse(departmentCode);
        if(sysBaseAccounts != null || !sysBaseAccounts.isEmpty()){
            for(SysBaseAccount sysBaseAccount : sysBaseAccounts){
                sysBaseAccount.setDepartmentCode(null);
            }
            sysBaseAccounts = sysBaseAccountRepository.saveAll(sysBaseAccounts);
        }
    }
}
